class Solution {
  public:
    int get_size(ListNode* h) {
        int _size = 0;
        while (h) {
            _size++;
            h = h->next;
        }
        return _size;
    }
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {

        ListNode* h1 = pHead1;
        ListNode* h2 = pHead2;

        int h1_size = get_size(h1);
        int h2_size = get_size(h2);
        int n = abs(h1_size - h2_size);

        if (h1_size > h2_size) {
            while (n--) {
                h1 = h1->next;
            }
        } else {
            while (n--) {
                h2 = h2->next;
            }
        }
        while (h1 && h2) {
            if (h1->val == h2->val) {
                return h1;
            } else {
                h1 = h1->next;
                h2 = h2->next;
            }
        }

        return nullptr;
    }
};